package com.maistars.spider.service.process.html.jsoup;

import com.maistars.spider.service.domain.rule.FieldRule;
import com.maistars.spider.service.parser.policy.ParserPolicyType;
import com.maistars.spider.service.parser.policy.impl.XPathPolicy;
import com.maistars.spider.service.process.html.HtmlProcessor;
import com.maistars.spider.service.process.html.HtmlProcessorConfig;
import org.jsoup.Jsoup;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dhr
 * @date 2021/2/22 下午2:12
 */
public class HtmlBeginProcessor extends HtmlProcessor {

    public HtmlBeginProcessor(String htmlContent, FieldRule fieldRule, HtmlProcessorConfig htmlProcessorConfig, Map<String, Object> processorContext) {
        super(htmlContent,fieldRule,htmlProcessorConfig, processorContext);
    }

    @Override
    public HtmlProcessor init() {
        if(fieldRule.getParseType() == ParserPolicyType.XPATH.getValue()){
            XPathPolicy parserPolicy = new XPathPolicy();
            parserPolicy.init(htmlContent);
            this.processorContext.put("orignalDocument",Jsoup.parse(htmlContent));
            this.processorContext.put("processedElements",parserPolicy.executeWithElement(fieldRule.getRule(),new HashMap<>()));
        }
        return this;
    }

    @Override
    public List<String> process(List<String> contents) {
        return contents;
    }
}
